package function.waterMark;

/**
 * @author cuihao
 * @Title: 给图片加图片和文字水印
 * @date 2018/8/1519:35
 */

import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;


public class WaterMarkUtil {

    private static Color baseColor = new Color(254, 254, 254, 255);

    private static Color deepColor = new Color(34, 34, 34, 255);
    private static Color lightColor = new Color(130, 130, 130, 255);

    private static Font bigFont = new Font("黑体", Font.PLAIN, 42);
    private static Font smallFont = new Font("黑体", Font.PLAIN, 20);

    private static Font phoneFont = new Font("黑体", Font.PLAIN, 24);

    private static Logger logger = Logger.getLogger(WaterMarkUtil.class);

    /**
     * @param baseImgPath    底图
     * @param backImgPath    背景图
     * @param erweimaImgPath 二维码
     * @param headImgPath    头像
     * @param storeName      门店名
     * @param storePhone     门店联系电话
     * @param tarImgPath
     */
    public static void addWaterMark(String baseImgPath, String backImgPath, String erweimaImgPath, String headImgPath,
                                    String storeName, String storePhone, String tarImgPath) {

        try {
            /* 获取图片信息 start*/
            // 读取底图图片信息
            int baseImgWidth = 604;//底图的宽
            int baseImgHeight = 964;//底图的高

            // 读取背景图信息
            File backImgFile = new File(backImgPath);
            Image backImg = ImageIO.read(backImgFile);
            int backImgWidth = backImg.getWidth(null);//获取图片的宽
            int backImgHeight = backImg.getHeight(null);//获取图片的高

            // 读取二维码信息
            File erweimaImgFile = new File(erweimaImgPath);
            Image erweimaImg = ImageIO.read(erweimaImgFile);
            int erweimaImgWidth = erweimaImg.getWidth(null);//获取图片的宽
            int erweimaImgHeight = erweimaImg.getHeight(null);//获取图片的高

            // 读取头像图片信息
            File headImgFile = new File(headImgPath);
            Image headImg = ImageIO.read(headImgFile);
            int headImgWidth = headImg.getWidth(null);//获取图片的宽
            int headImgHeight = headImg.getWidth(null);//获取图片的高

            /* 合成图片 start */
            // 整体合成图
            BufferedImage bufImg = new BufferedImage(baseImgWidth, baseImgHeight, BufferedImage.TYPE_INT_RGB);

            // 绘制底图
            Graphics2D baseg = bufImg.createGraphics();
            baseg.drawImage(null, 0, 0, baseImgWidth, baseImgHeight, null);
            baseg.setColor(baseColor);
            baseg.fillRect(0, 0, baseImgWidth, baseImgHeight);

            // 绘制文字
            Graphics2D nameg = bufImg.createGraphics();
            nameg.setColor(deepColor);
            nameg.setFont(bigFont);
            nameg.drawString(storeName, 30, 80);

            Graphics2D headtg = bufImg.createGraphics();
            headtg.setColor(lightColor);
            headtg.setFont(smallFont);
            headtg.drawString("超级网络门店", 30, 120);

            // 绘制头像
            int headx = baseImgWidth - 100;
            Graphics2D headg = bufImg.createGraphics();
            Ellipse2D.Double shape = new Ellipse2D.Double(headx, 50, 65, 65);
            headg.setClip(shape);
            headg.drawImage(headImg, headx, 50, 65, 65, null);

            // 绘制背景图
            int backy = 80 + 65 + 20;
            int backw = baseImgWidth - 60;
            int backh = baseImgHeight - 80 - 65 - 20 - 200;
            Graphics2D backg = bufImg.createGraphics();
            backg.drawImage(backImg, 30, backy, backw, backh, null);

            //绘制二维码
            int erweimay = backy + backh + 20;
            Graphics2D erweimag = bufImg.createGraphics();
            erweimag.drawImage(erweimaImg, 50, erweimay, 170, 170, null);

            //绘制电话号码
            Graphics2D phoneg = bufImg.createGraphics();
            phoneg.setColor(deepColor);
            phoneg.setFont(phoneFont);
            phoneg.drawString(storePhone, 50 + 150 + 50, erweimay + 60);

            Graphics2D phonetg = bufImg.createGraphics();
            phonetg.setColor(lightColor);
            phonetg.setFont(smallFont);
            phonetg.drawString("长按识别二维码到我门店看看", 50 + 150 + 50, erweimay + 100);

            //g.dispose();

            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            logger.info("水印添加完成");
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            logger.info("水印添加失败");
            e.printStackTrace();
        }
    }

    /**
     * 获取文字的宽度
     *
     * @param waterMarkContent
     * @param g
     * @return
     */
    private static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
}
